package cn.ekuma.odbo.mobile.ui

import android.annotation.SuppressLint
import android.app.Application
import androidx.arch.core.executor.ArchTaskExecutor
import androidx.paging.DataSource
import androidx.paging.PagedList
import cn.ekuma.odbo.mobile.helper.PageListBrowsEditableHelper
import cn.ekuma.odbo.mobile.room.BaseDao

abstract class AbstractPagedListEditorViewModel<T : Any,KEY : Any>(application: Application) :AbstractOdboEditorViewModel<T,KEY>(application){

    lateinit var pagedList:PagedList<T>

    abstract fun buildDataSourse(): DataSource<KEY, T>

    abstract fun getBaseDao(): BaseDao<T>

    @SuppressLint("RestrictedApi")
    fun buildEntityPageList(): PagedList<T> {
        return PagedList.Builder(buildDataSourse(), PagedList.Config.Builder().setPageSize(10).build())
                .setNotifyExecutor(ArchTaskExecutor.getMainThreadExecutor())
                .setFetchExecutor(ArchTaskExecutor.getIOThreadExecutor())
                .build()
    }

    override fun buildBrowsablehelper(): PageListBrowsEditableHelper<T> {
        pagedList=buildEntityPageList()
        return PageListBrowsEditableHelper(pagedList, baseDao = getBaseDao())
    }

}